#include "MOSelectionWidget.h"
#include "resources.h"

#include "../definitions.h"
#include "../Engine.h"

#include <QHeaderView>

namespace ui {

MOSelectionWidget::MOSelectionWidget(QWidget* _parent): BaseUI() {
    parent = _parent;
}

bool MOSelectionWidget::setup() {
    if(!BaseUI::setup(UI_MO_SELECTION_WIDGET))
        return false;

    return true;
}

void MOSelectionWidget::populateLists() {
    const core::MOStorage& lst = core::Engine::getInstance().getMOList();

    measurement::MeasObjIndex nDev = 0, nProc = 0, nViz = 0;
    measurement::MeasObjIndex *index;

    QTableWidgetItem *item;
    QTableWidgetItem *check;
    QTableWidget *currentTab;

    QTableWidget *tabDevices        = formWidget->findChild<QTableWidget*>("tabDevices");
    QTableWidget *tabProcessors     = formWidget->findChild<QTableWidget*>("tabProcessors");
    QTableWidget *tabVisualizations = formWidget->findChild<QTableWidget*>("tabVisualizations");

#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    tabDevices->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    tabProcessors->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    tabVisualizations->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
#else
    tabDevices->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    tabProcessors->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    tabVisualizations->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
#endif

    if(tabDevices->rowCount() > 0) tabDevices->clearContents();
    if(tabProcessors->rowCount() > 0) tabProcessors->clearContents();
    if(tabVisualizations->rowCount() > 0) tabVisualizations->clearContents();

    for(core::MOStorage::const_iterator it = lst.begin();
        it != lst.end(); ++it) {

        switch((*it)->type) {
        case measurement::DataSource:
            currentTab = tabDevices;
            index = &nDev;
            break;
        case measurement::DataProcessor:
            currentTab = tabProcessors;
            index = &nProc;
            break;
        case measurement::Visualization:
            currentTab = tabVisualizations;
            index = &nViz;
            break;
        }

        currentTab->insertRow(currentTab->rowCount());

        check = new QTableWidgetItem;
        check->setCheckState(Qt::Unchecked);
        currentTab->setItem(*index, 0, check);

        item = new QTableWidgetItem(QString::fromStdWString((*it)->name));
        item->setData(Qt::UserRole, (*it)->index);

        currentTab->setItem(*index, 1, item);
        currentTab->setItem(*index, 2, new QTableWidgetItem(
                                QString::fromStdWString((*it)->author)));
        currentTab->setItem(*index, 3, new QTableWidgetItem(
                                QString::fromStdString((*it)->path)));

        ++(*index);
    }
}

void MOSelectionWidget::getSelected(QTableWidget *tab, IndexLst &lst) {
    QTableWidgetItem *item;

    for(measurement::MeasObjIndex index = 0; index < tab->rowCount(); ++index) {
        item = tab->item(index, 0);
        if(item->checkState() == Qt::Checked) {
            item->setCheckState(Qt::Unchecked);

            item = tab->item(index, 1);
            lst.push_back(item->data(Qt::UserRole).toUInt());
        }
    }
}

IndexLst MOSelectionWidget::getSelected() {
    IndexLst selected;

    QTableWidget *tabDevices        = formWidget->findChild<QTableWidget*>("tabDevices");
    QTableWidget *tabProcessors     = formWidget->findChild<QTableWidget*>("tabProcessors");
    QTableWidget *tabVisualizations = formWidget->findChild<QTableWidget*>("tabVisualizations");

    getSelected(tabDevices, selected);
    getSelected(tabProcessors, selected);
    getSelected(tabVisualizations, selected);

    return selected;
}

}
